package com.suxii.frpclient.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.suxii.frpclient.R;
import com.suxii.frpclient.databinding.FragmentHomeBinding;
import com.suxii.frpclient.message.ServiceStateMessage;
import com.suxii.frpclient.service.FrpService;
import com.suxii.frpclient.utils.RxBus;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;


public class HomeFragment extends Fragment {

    public HomeFragment() {

    }

    public static final String NAME = "HOME";
    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    private FragmentHomeBinding binding;
    private final CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        initEvent();
        return view;
    }

    private boolean state;

    private void initEvent(){
        binding.stateBg.setOnClickListener(v -> handleState());

        // 订阅 RxBus 中的数据
        Disposable disposable = RxBus.getInstance().toObservable(ServiceStateMessage.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(data -> {
                    state = data.isState();
                    if (state){
                        binding.stateBg.setBackgroundResource(R.drawable.bg_home_start);
                        binding.stateText.setText("已开启");
                    }else {
                        binding.stateBg.setBackgroundResource(R.drawable.bg_home_shutdown);
                        binding.stateText.setText("已关闭");
                    }
                });

        compositeDisposable.add(disposable);
    }

    private void handleState(){
        Intent intent = new Intent(requireContext(), FrpService.class);
        if (!state){
            binding.stateBg.setBackgroundResource(R.drawable.bg_home_start);
            binding.stateText.setText("已开启");
            ContextCompat.startForegroundService(requireContext(),intent);
            Toast.makeText(requireContext(), "开启服务", Toast.LENGTH_SHORT).show();
        }else {
            binding.stateBg.setBackgroundResource(R.drawable.bg_home_shutdown);
            binding.stateText.setText("已关闭");
            boolean stop = requireContext().stopService(intent);
            Toast.makeText(requireContext(), "关闭服务："+stop, Toast.LENGTH_SHORT).show();
        }
//        state = !state;
    }


    @Override
    public void onDestroy() {
        compositeDisposable.clear();
        super.onDestroy();
    }
}